package tankgame01;

import java.util.Vector;

public class Tank {
    private int x;
    private int y;
    private int direction = 0;
    private int speed = 7;
    public boolean isLive = true;
    public static Vector walls = new Vector();

    public static Vector getWalls() {
        return walls;
    }

    public static void setWalls(Vector walls1) {
        walls = walls1;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public void moveUp() {
        if (y >= speed) {
            if(!isTouchWall()) {
                y -= speed;
            }
        } else {
            y = 0;
        }
    }

    public void moveRight() {
        if (x <= 940 - speed) {
            if(!isTouchWall()){
            x += speed;
            }
        } else {
            x = 940;
        }
    }

    public void moveDown() {
        if (y <= 690 - speed) {
            if(!isTouchWall()) {
                y += speed;
            }
        } else {
            y = 690;
        }
    }

    public void moveLeft() {
        if (x >= speed) {
            if(!isTouchWall()) {
                x -= speed;
            }
        } else {
            x = 0;
        }
    }

    public int getDirection() {
        return direction;
    }

    public void setDirection(int direction) {
        this.direction = direction;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Tank(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public  boolean isTouchWall() {
        synchronized (walls) {
            for (Object o : walls) {
                Wall wall = (Wall) o;
                if (this.getDirection() == 0) {
                    if (wall.getX() >= getX() && wall.getX() <= getX() + 40
                            && wall.getY() + 10 >= getY() && wall.getY() + 10 <= getY() + 60) {
                        return true;
                    }
                    if (wall.getX() + 10 >= getX() && wall.getX() + 10<= getX() + 40
                            && wall.getY() + 10 >= getY() && wall.getY() + 10 <= getY() + 60) {
                        return true;
                    }
                }
                if (this.getDirection() == 1) {
                    if (wall.getX() >= getX() && wall.getX() <= getX() + 60
                            && wall.getY()  >= getY() && wall.getY()  <= getY() + 40) {
                        return true;
                    }
                    if (wall.getX()  >= getX() && wall.getX() <= getX() + 60
                            && wall.getY() + 10 >= getY() && wall.getY() + 10 <= getY() + 40) {
                        return true;
                    }
                }
                if (this.getDirection() == 2) {
                    if (wall.getX() >= getX() && wall.getX() <= getX() + 40
                            && wall.getY() >= getY() && wall.getY() <= getY() + 60) {
                        return true;
                    }
                    if (wall.getX() + 10 >= getX() && wall.getX() + 10<= getX() + 40
                            && wall.getY()  >= getY() && wall.getY()<= getY() + 60) {
                        return true;
                    }
                }
                if (this.getDirection() == 3) {
                    if (wall.getX() + 10 >= getX() && wall.getX() + 10 <= getX() + 60
                            && wall.getY()  >= getY() && wall.getY()  <= getY() + 40) {
                        return true;
                    }
                    if (wall.getX() + 10 >= getX() && wall.getX() + 10 <= getX() + 60
                            && wall.getY() + 10 >= getY() && wall.getY() + 10 <= getY() + 40) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
